<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>从数组中随机取出一个或多个随机键</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.array-push.html">« array_push</a></li>
      <li style="float: right;"><a href="function.array-reduce.html">array_reduce »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.array.html">数组 函数</a></li>
    <li>从数组中随机取出一个或多个随机键</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.array-rand" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">array_rand</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">array_rand</span> &mdash; <span class="dc-title">从数组中随机取出一个或多个随机键</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.array-rand-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>array_rand</strong></span>(<span class="methodparam"><span class="type">array</span> <code class="parameter">$array</code></span>, <span class="methodparam"><span class="type">int</span> <code class="parameter">$num</code><span class="initializer"> = 1</span></span>): <span class="type"><span class="type">int</span>|<span class="type">string</span>|<span class="type">array</span></span></div>

  <p class="para rdfs-comment">
   从数组中取出一个或多个随机的单元，并返回随机条目对应的键（一个或多个）。
  </p>
  <div class="caution"><strong class="caution">警告</strong>
 <p class="para">
  本函数并不会生成安全加密的值，并且<em class="emphasis">不可</em>用于加密或者要求返回值不可猜测的目的。
 </p>
 <p class="para">
  如果需要加密安全随机，则可以将 <span class="classname"><a href="class.random-engine-secure.html" class="classname">Random\Engine\Secure</a></span> 引擎用于
  <span class="classname"><a href="class.random-randomizer.html" class="classname">Random\Randomizer</a></span>。对于简单的用例，<span class="function"><a href="function.random-int.html" class="function">random_int()</a></span>
  和 <span class="function"><a href="function.random-bytes.html" class="function">random_bytes()</a></span> 函数提供了操作系统的 <abbr title="Cryptographically Secure PseudoRandom Number Generator">CSPRNG</abbr> 支持的方便且安全的 <abbr title="Application Programming Interface">API</abbr>。
 </p>
</div>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.array-rand-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">array</code></dt>

     <dd>

      <p class="para">
       输入的数组。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">num</code></dt>

     <dd>

      <p class="para">
       指定要取出的单元数量。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.array-rand-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   如果只取出一个，<span class="function"><strong>array_rand()</strong></span>
   返回随机单元的键名。
   否则就返回包含随机键名的数组。
   完成后，就可以根据随机的键获取数组的随机值。
   如果返回的是包含随机键名的数组，数组单元的顺序按照键名在原数组中的顺序排列。
   取出数量如果超过 array 的长度，就会导致 <strong><code>E_WARNING</code></strong> 错误，并返回 NULL。
  </p>
 </div>

 <div class="refsect1 changelog" id="refsect1-function.array-rand-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>7.1.0</td>
       <td>
        内部随机化算法已从 libc rand 函数<a href="migration71.incompatible.html#migration71.incompatible.rand-srand-aliases" class="link">更改为</a>使用
        <a href="http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html" class="link external">&raquo;&nbsp;梅森旋转算法（Mersenne Twister）</a> 随机数生成器。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.array-rand-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4945">
    <p><strong>示例 #1 <span class="function"><strong>array_rand()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$input&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"Neo"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"Morpheus"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"Trinity"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"Cypher"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"Tank"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$rand_keys&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_rand</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$input</span><span style="color: #007700">[</span><span style="color: #0000BB">$rand_keys</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">$input</span><span style="color: #007700">[</span><span style="color: #0000BB">$rand_keys</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.array-rand-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="random-randomizer.pickarraykeys.html" class="function" rel="rdfs-seeAlso">Random\Randomizer::pickArrayKeys()</a> - Select random array keys</span></li>
    <li class="member"><span class="function"><a href="random-randomizer.shufflearray.html" class="function" rel="rdfs-seeAlso">Random\Randomizer::shuffleArray()</a> - Get a permutation of an array</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>